#!/usr/bin/python3
import redis
from flask import Blueprint

import os
from flask import Flask, request, jsonify, render_template
import json
import sys


class Token:
    def __init__(self):
        self.r = None
        pass
    def open(self, db=0, port=6379):
        self.r = redis.Redis(host='localhost', port=6379, db=db)
    # 某token是否存在
    def check(self, key):
        return self.r.exists(key) == 1
    # 从持久化mysql存入redis
    def add(self, token):
        return True

    # 从redis删除token，基本上用不着
    def remove(self, token):
        return True





# token_r = Blueprint('token_r', __name__)
token_r = Blueprint('token', __name__)

# ajax版本
@token_r.route('/token', methods=['POST'])
def check1():
    # 从 AJAX 请求中获取数据
    data = request.json
    
    # 处理数据
    token_val = data.get('token')
    token = Token()
    token.open(0)
    print(token.check(token_val))
    
    return jsonify({
        'message': '-',
        'success': True,
        "val": token.check(token_val),
        'status_code': '1',
    })




def _test():
    # token
    token = Token()
    token.open(0)
    print(token.check("name2"))
    print(token.check("name23"))
    print(token.check("fff"))
    # print(token.check("name21"))
    pass


if __name__ == '__main__':
    _test();
    # token = Token()
    # print(token.check("22"))
    # print(token.check("name21"))